﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using ERP_RORZE_SYSTEM;
using System.Data.OleDb;
using System.Data.Common;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ERP_RORZE_DLL.Assembly
{
    public partial class panel_F4LocalTroubles : UserControl
    {
        private INISettings mySetting;
        private SqlConnection myConnection;
        public panel_F4LocalTroubles()
        {
            InitializeComponent();
            mySetting = new INISettings();
            myConnection = new SqlConnection(mySetting.SQLConnectionString);
            LoadData();
            this.F4LocalTroubles.DoubleClick += F4LocalTroubles_DoubleClick;
            this.F4LocalTroubles.PopupMenuShowing += F4LocalTroubles_PopupMenuShowing;
            this.f4F4LocalTroublesContextMenu.ItemClicked += f4LocalTroublesContextMenu_ItemClicked;
        }

        void f4LocalTroublesContextMenu_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {
            this.f4F4LocalTroublesContextMenu.Hide();
            DataRow row = F4LocalTroubles.GetFocusedDataRow();
            switch (e.ClickedItem.Name)
            {
                case "editSelection":                    
                    frm_Edit_F4LocalTroubles editForm = new frm_Edit_F4LocalTroubles();
                    editForm.troubleID = Int32.Parse(row[0].ToString());
                    if (editForm.LoadData())
                    {
                        editForm.Show();
                    } 
            
                    break;
                case "addSelection":
                    frm_Add_F4LocalTroubles add_Form = new frm_Add_F4LocalTroubles();
                    add_Form.Show();
                    break;                
                case "refreshSelection":
                    LoadData();
                    break;
            }
        }

        void F4LocalTroubles_PopupMenuShowing(object sender, DevExpress.XtraGrid.Views.Grid.PopupMenuShowingEventArgs e)
        {
            this.f4F4LocalTroublesContextMenu.Show(MousePosition);
        }

        private void F4LocalTroubles_DoubleClick(object sender, EventArgs e)
        {            
            
            DataRow row = F4LocalTroubles.GetFocusedDataRow();
            frm_Edit_F4LocalTroubles editForm = new frm_Edit_F4LocalTroubles();
            editForm.troubleID = Int32.Parse(row[0].ToString());
            if (editForm.LoadData())
            {
                editForm.Show();
            } 
        }
        
        //////////////////////////////////////////////////
        /// Load data from database to grid
        //////////////////////////////////////////////////
        private void LoadData()
        {
            DataSet sourceDataSet = new DataSet();
            SQLManagement mySQLManagement = new SQLManagement();
            mySQLManagement.SQLConnectionString = mySetting.SQLConnectionString;
            string sql = "SELECT [231_F4Troubles].ID, [231_F4Troubles].Date, [231_F4Troubles].TimePlanNo,"
                + " [231_TimePlan].CompID, [231_F4Troubles].SerialNo,"
                + " [231_F4Troubles].Quantity, [222_Staff].StaffName, [222_Staff].GroupID,"
                + " [231_F4Troubles].F4TroubleContent, [231_F4Troubles].Reason, [231_F4Troubles].Solution, [231_F4Troubles].Remark"
                + " FROM [231_F4Troubles] LEFT OUTER JOIN"
                + " [231_TimePlan] ON [231_TimePlan].TimePlanNo=[231_F4Troubles].TimePlanNo LEFT OUTER JOIN"
                + " [222_Staff] ON [222_Staff].StaffID=[231_F4Troubles].Checker"
                + " WHERE [231_F4Troubles].Type=0"
                + " ORDER BY [231_F4Troubles].Date";
            sourceDataSet = mySQLManagement.SQL_Query(sql);
            if (sourceDataSet != null)
            {
                gridF4LocalTroubles.DataSource = sourceDataSet.Tables[0];
            }
            else
            {
                DataTable newTable = new DataTable();
                newTable.Columns.Add("ID", typeof(string));
                newTable.Columns.Add("Date", typeof(DateTime));
                newTable.Columns.Add("TimePlanNo", typeof(string));
                newTable.Columns.Add("Product", typeof(string));                
                newTable.Columns.Add("SerialNo", typeof(string));
                newTable.Columns.Add("Quantity", typeof(int));
                newTable.Columns.Add("Checker", typeof(string));
                newTable.Columns.Add("GroupID", typeof(string));
                newTable.Columns.Add("F4TroubleContent", typeof(string));                
                newTable.Columns.Add("Reason", typeof(string));                
                newTable.Columns.Add("Solution", typeof(string));
                newTable.Columns.Add("Remark", typeof(string));
                gridF4LocalTroubles.DataSource = newTable;
            }
            F4LocalTroubles.Columns[0].Caption = "ID";
            F4LocalTroubles.Columns[1].Caption = "Date";
            F4LocalTroubles.Columns[2].Caption = "Time Plan No.";
            F4LocalTroubles.Columns[3].Caption = "Product";            
            F4LocalTroubles.Columns[4].Caption = "Serial No.";
            F4LocalTroubles.Columns[5].Caption = "Quantity";
            F4LocalTroubles.Columns[6].Caption = "Checker";
            F4LocalTroubles.Columns[7].Caption = "Group";
            F4LocalTroubles.Columns[8].Caption = "Trouble Content";
            F4LocalTroubles.Columns[9].Caption = "Reason";
            F4LocalTroubles.Columns[10].Caption = "Solution";
            F4LocalTroubles.Columns[11].Caption = "Remark";
            F4LocalTroubles.Columns[0].Visible = false;
            F4LocalTroubles.BestFitColumns();
        }

        private void bottonAdd_Click(object sender, EventArgs e)
        {            
            frm_Add_F4LocalTroubles add_Form = new frm_Add_F4LocalTroubles();
            add_Form.Show();
        }

        private void bottonEdit_Click(object sender, EventArgs e)
        {            
            DataRow row = F4LocalTroubles.GetFocusedDataRow();
            frm_Edit_F4LocalTroubles editForm = new frm_Edit_F4LocalTroubles();
            editForm.troubleID = Int32.Parse(row[0].ToString());
            if (editForm.LoadData())
            {
                editForm.Show();
            } 
        }
    }
}
